package kosta.controller;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kosta.model.vo.AuctionVO;
import kosta.model.vo.BidVO;
import kosta.model.vo.BigClassVO;
import kosta.model.vo.ListVO;
import kosta.model.vo.MemberVO;
import kosta.model.vo.MessageVO;
import kosta.model.vo.SearchVO;
import kosta.model.vo.SmallClassVO;
import kosta.service.AdminService;
import kosta.service.AuctionService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Resource(name="adminServiceImpl")
	private AdminService adminService;
	@Resource(name="auctionServiceImpl")
	private AuctionService auctionService;
	
	@RequestMapping("/adminMemberList.do") 
	public ModelAndView adminMemberList(HttpServletRequest request,SearchVO svo ) {	
    	System.out.println("adminMemberList "+svo);
		return new ModelAndView("adminMemberList.admin","lvo",adminService.getMemberList(svo));
	}
	
    //회원 등급 조정
    @RequestMapping("/updataMemberGrade.do") 
    @ResponseBody
	public int updataMemberGrade(MemberVO mvo) {	
    	adminService.updateMemberGrade(mvo); 
    	System.out.println(mvo);
		return mvo.getGrade();
	}
    
    //admin에서 회원 강퇴
    @RequestMapping("/adminMemberOut.do") 
	public String adminMemberOut(MemberVO mvo ) {	
    	adminService.adminMemberOut(mvo);
		return "redirect:adminMemberList.do?flag=memberNo";
	}
    //관리자가 삭제 요청된 게시물에서 삭제하면 상품테이블, 삭제요청테이블에서 삭제하고 등록자에게
    //쪽지보냄
    @RequestMapping("/adminDeleteAuction.do")     
    public String adminDelete(HttpServletRequest request){    	
    	int boardNo = Integer.parseInt(request.getParameter("boardNo"));
    	int deleteConfirmNo = Integer.parseInt(request.getParameter("deleteConfirmNo"));
    	String receiveMemberId=request.getParameter("receiveMemberId");
    	String sendMemberId=request.getParameter("sendMemberId");
    	int receiveMemberNo = Integer.parseInt(request.getParameter("receiveMemberNo"));
    	List<BidVO> list=auctionService.bid(boardNo);
    	System.out.println("삭제시 : "+receiveMemberId);
    	if(list.size()>=5){
			adminService.downGrade(receiveMemberId);
		}
    	HashMap map = new HashMap();
    	map.put("boardNo", boardNo);
    	map.put("deleteConfirmNo", deleteConfirmNo);
    	MessageVO msvo = new MessageVO();
    	msvo.setMessageTitle(boardNo+"번 게시물 삭제처리 되었습니다.");
    	msvo.setReceiveMemberId(receiveMemberId);
    	msvo.setSendMemberId(sendMemberId);  
    	msvo.setReceiveMemberNo(receiveMemberNo);
    	msvo.setMessageContent("복구는 불가능합니다.");
    	adminService.adminDelete(map, msvo);    
    	return "redirect:deleteRequestCheckList.do";
    }
    
    //삭제요청된 게시물 관리자가 거절했을 때 실행되는 메소드.
    //게시물은 활성화. 거절쪽지 보냄. 삭제요청테이블에선 삭제.
    @RequestMapping("/adminNotDeleteAuction.do")     
    public String adminNotDeleteAuction(HttpServletRequest request){    	
    	int boardNo = Integer.parseInt(request.getParameter("boardNo"));
    	int deleteConfirmNo = Integer.parseInt(request.getParameter("deleteConfirmNo"));
    	String receiveMemberId=request.getParameter("receiveMemberId");
    	String sendMemberId=request.getParameter("sendMemberId");
    	int receiveMemberNo = Integer.parseInt(request.getParameter("receiveMemberNo"));
    	List<BidVO> list=auctionService.bid(boardNo);
    	System.out.println("삭제시 : "+receiveMemberId);
    
    	HashMap map = new HashMap();
    	map.put("boardNo", boardNo);
    	map.put("deleteConfirmNo", deleteConfirmNo);
    	MessageVO msvo = new MessageVO();
    	msvo.setMessageTitle(boardNo+"번 게시물 삭제요청 거절!!");
    	msvo.setReceiveMemberId(receiveMemberId);
    	msvo.setSendMemberId(sendMemberId);  
    	msvo.setReceiveMemberNo(receiveMemberNo);
    	msvo.setMessageContent("자세항 사항은 관리자에게 문의하세요.");
    	adminService.refuseAdminDelete(map,msvo);    
    	return "redirect:deleteRequestCheckList.do";
    }
    
    @RequestMapping("/deleteRequestCheckList.do")  
    public ModelAndView deleteRequestCheckList(SearchVO svo){
    	 ListVO lvo = adminService.deleteRequestCheckList(svo);	  
    	 System.out.println(lvo+"나와");
    	 return new ModelAndView("deleteRequestCheckList.admin","lvo",lvo);
    }
    
   @RequestMapping("/adminShowContent.do")
	public ModelAndView showContent(HttpServletRequest request,
			HttpServletResponse response){
		int boardNo=Integer.parseInt(request.getParameter("boardNo"));
		AuctionVO avo=null;
		
		Cookie[] cookies=request.getCookies();		
		if(cookies==null || cookies.length==0){
			System.out.println ("1.쿠키가 존재하지 않은 상태..myboard cookie 전송");
			response.addCookie(new Cookie("myboard","|"+boardNo+"|"));
			//조회수 증가 			
			avo=auctionService.findAuctionInfo(boardNo);
		}else{
			Cookie myboardCookie=null;
			for(int i=0;i<cookies.length;i++){
				if(cookies[i].getName().equals("myboard")){
					myboardCookie=cookies[i];
					break;
				}
			}
			if(myboardCookie==null){
				System.out.println ("2.쿠키가 존재하지만 myboard 쿠키가 존재하지 않은 상태..myboard cookie 전송");
				response.addCookie(new Cookie("myboard","|"+boardNo+"|"));
				//조회수 증가 				
				avo=auctionService.findAuctionInfo(boardNo);
			}else{
				System.out.println ("3.쿠키가 존재하고 myboard 쿠키가 존재하는 상태..");
				String value=myboardCookie.getValue();
				if(value.indexOf("|"+boardNo+"|")!=-1){
					System.out.println ("3.1 해당 게시글 번호가 존재..조회수 증가하지 않는다");
					//조회수 증가 X
					//vo=boardService.getPostingByNoNotHit(no);				
					avo=auctionService.findAuctionInfoNotHit(boardNo);
					
				}else{// -1 즉 존재하지 않으면 
					System.out.println ("3.2 해당 게시글 번호가 존재X..조회수 증가하고 value업데이트 후 전송");
					value+="|"+boardNo+"|";
					response.addCookie(new Cookie("myboard",value));
					//조회수 증가 
					avo=auctionService.findAuctionInfo(boardNo);				
				}
			}
		}		
		
		MemberVO mvo=auctionService.findMemberInfo(avo.getMemberNo());
		SmallClassVO scvo=auctionService.findSmallClass(avo.getBoardNo());
		List<BidVO> bdvo=auctionService.bid(boardNo);
		System.out.println("입찰내역"+bdvo);
		BigClassVO bcvo=auctionService.findBigClass(scvo.getBigClassNo());
		HashMap map=new HashMap();
		map.put("avo", avo);
		map.put("mvo",mvo);
		map.put("scvo", scvo);
		map.put("bcvo", bcvo);
		map.put("bdvo",bdvo);
		return new ModelAndView("adminShowContent.admin","vo",map);
	}
   
   @RequestMapping("/adminAuctionUpdateForm.do")
   public ModelAndView auctionUpdateForm(HttpServletRequest  request){
      int boardNo=Integer.parseInt(request.getParameter("boardNo"));
      AuctionVO avo=auctionService.findAuctionInfo(boardNo);   
      MemberVO mvo=auctionService.findMemberInfo(avo.getMemberNo());
      SmallClassVO scvo=auctionService.findSmallClass(avo.getBoardNo());
      BigClassVO bcvo=auctionService.findBigClass(scvo.getBigClassNo());
      HashMap map=new HashMap();
      map.put("avo", avo);
      System.out.println("avo"+avo);
      map.put("mvo",mvo);
      System.out.println("mvo"+mvo);
      map.put("scvo", scvo);
      System.out.println("scvo"+scvo);
      map.put("bcvo", bcvo);
      System.out.println("bcvo"+bcvo);
      return new ModelAndView("adminAuctionUpdateForm.admin","vo",map);
   }
    
}
